<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\stat\daily;

use app\model\ClientAccountTypeDailyStatModel;
use think\Model;

class ClientAccountTypeService extends DailyService implements DailyInterface
{
    /**
     * @var Model 媒体日流水模型
     */
    public Model $data;

    /**
     * @var Model 汇总表模型
     */
    public Model $statDate;

    /**
     * 设置数据.
     */
    public function setData(Model $model): bool
    {
        $this->data = $model;
        return true;
    }

    /**
     * 过滤数据.
     * @return mixed
     */
    public function filter()
    {
        $statData = $this->getDailyModel($this->data->stat_date, $this->data->client_id, $this->data->media_id, $this->data->account_type);
        if (! $statData instanceof ClientAccountTypeDailyStatModel) {
            return $this->setError('汇总模型有误');
        }
        $this->statDate = $statData;

        return true;
    }

    /**
     * 保存汇总数据.
     * @return bool
     */
    public function saveStatData()
    {
        $filterRes = $this->filter();
        if (! $filterRes) {
            return false;
        }
        $statData = $this->statDate;
        $cost = $this->data->cost;
        $rewardCost = $this->data->reward_cost;
        $cashCost = $this->data->cash_cost;
        $balance = $this->data->balance;
        if (! $statData->getKey()) {
            $data = [
                'advertiser_id' => $this->data->advertiser_id,
                'client_id' => $this->data->client_id,
                'media_id' => $this->data->media_id,
                'account_type' => $this->data->account_type,
                'stat_date' => $this->data->stat_date,
                'balance' => $balance,
                'cash_cost' => $cashCost,
                'cost' => $cost,
                'reward_cost' => $rewardCost,
            ];
            $statData->save($data);
        } else {
            if ($statData->cost != $cost || $statData->cash_cost != $cashCost || $statData->reward_cost != $rewardCost || $statData->balance != $balance) {
                $statData->cost = $cost;
                $statData->cash_cost = $cashCost;
                $statData->reward_cost = $rewardCost;
                $statData->balance = $balance;
                $statData->save();
            }
        }
        return true;
    }

    /**
     * 获取当前汇总表数据是否存在.
     * @param int $statDate 日期
     * @return array|ClientAccountTypeDailyStatModel|mixed|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDailyModel(int $statDate, ...$arg)
    {
        $where = [
            ['client_id', '=', $arg[0]],
            ['stat_date', '=', $statDate],
        ];
        if (isset($arg[1])) {
            $where[] = ['media_id', '=', $arg[1]];
            if (isset($arg[2])) {
                $where[] = ['account_type', '=', $arg[2]];
            }
        }
        $res = ClientAccountTypeDailyStatModel::where($where)->find();
        return $res ? $res : new ClientAccountTypeDailyStatModel();
    }

    /**
     * 归档.
     */
    public function archive(int $statDate)
    {
    }
}
